$(function() {

    getbook()
    $('.btn-ensure').on('click', function(e) {
        if ($('#bookn').val() == '' || $('#booka').val() == '' || $('#bookp').val() == '') {
            // return alert('请输入正确的格式！');
            return $('.bs-example-modal-sm').modal();

        } else {
            var bookname = $('#bookn').val().trim();
            var author = $('#booka').val().trim();
            var publisher = $('#bookp').val().trim();
            console.log(bookname + ',' + author + ',' + publisher);
            $.post('http://www.liulongbin.top:3006/api/addbook', {
                bookname: bookname,
                author: author,
                publisher: publisher
            }, function(res) {
                if (res.status == 201) {
                    // alert('添加成功');
                    $('#bookn').val('');
                    $('#booka').val('');
                    $('#bookp').val('');
                }
            })
            console.log(bookname);
            getbook()
        }
    });






    function getbook() {
        $.get('http://www.liulongbin.top:3006/api/getbooks', function(res) {
            if (res.status == 200) {
                var arr = [];
                $.each(res.data, function(i, ele) {
                    arr.push('<tr><td>' + res.data[i].id + '</td><td>' + res.data[i].bookname + '</td><td>' + res.data[i].author + '</td><td>' + res.data[i].publisher + '</td><td><a href="javascript:;" id="shanchu" data-index=' + res.data[i].id + '>删除</a></td></tr>');
                });

                $('#tb').empty().append(arr.join(''));
            };
        })
    }
    $('#tb').on('click', 'a', function() {
        var index = $(this).attr('data-index');
        console.log(index);
        $.get('http://www.liulongbin.top:3006/api/delbook', {
                id: index
            },
            function(res) {
                console.log(res.status);
            });
        getbook()
    })

});